package com.vpen.server.one.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.simp.stomp.StompCommand;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.messaging.support.ChannelInterceptor;
import org.springframework.messaging.support.MessageHeaderAccessor;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 描述: Channel通道拦截器
 *
 * @author 韦鹏
 * @date 2022/11/28 14:37
 */
@Slf4j
@Component
public class SocketChanelInterceptor implements ChannelInterceptor {

    /**
     * 消息发送Channel之前调用
     * 能获取Stomp连接的请求头Headers的信息
     */
    @Override
    public Message<?> preSend(Message<?> message, MessageChannel channel) {
        log.debug("发送之前调用：{},{}", message, channel);

        //将请求头的信息转换成这个实体类，不然就自己通过 message.getHeaders()
        StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
        log.debug("{}", accessor);

        if (accessor == null) {
            throw new RuntimeException("身份验证失败");
        }

        // 如果是连接类型
        if (StompCommand.CONNECT.equals(accessor.getCommand())) {
            String name = accessor.getFirstNativeHeader("name");
            if (!"vpen".equalsIgnoreCase(name)) {
                log.error("身份验证失败");
                throw new RuntimeException("身份验证失败");
            }
            //
            log.debug("登录成功后的一些操作....");
        }

        // 是否能订阅频道
        if (StompCommand.SUBSCRIBE.equals(accessor.getCommand())) {
            log.debug("校验权限");
        }
        // 是否能发送
        if (StompCommand.SEND.equals(accessor.getCommand())) {
            log.debug("发送操作....");
        }

        return ChannelInterceptor.super.preSend(message, channel);
    }

    /**
     * 消息发送Channel之后调用
     */
    @Override
    public void postSend(Message<?> message, MessageChannel channel, boolean sent) {
        log.debug("消息发送之后调用");
        ChannelInterceptor.super.postSend(message, channel, sent);
    }

    /**
     * Channel发送完成调用
     */
    @Override
    public void afterSendCompletion(Message<?> message, MessageChannel channel, boolean sent, Exception ex) {
        log.debug("发送完成调用");
        ChannelInterceptor.super.afterSendCompletion(message, channel, sent, ex);
    }

    /**
     * Channel接受消息之前调用
     */
    @Override
    public boolean preReceive(MessageChannel channel) {
        log.debug("接受消息之前调用");


        return ChannelInterceptor.super.preReceive(channel);
    }

    /**
     * Channel接受之后调用
     */
    @Override
    public Message<?> postReceive(Message<?> message, MessageChannel channel) {
        log.debug("接受之后调用");
        return ChannelInterceptor.super.postReceive(message, channel);
    }

    /**
     * Channel完成接受后调用
     */
    @Override
    public void afterReceiveCompletion(Message<?> message, MessageChannel channel, Exception ex) {
        log.debug("完成接受后调用");
    }
}
